﻿/**
 * @author by leili
 * AjaxValidator对象用于可以动态从服务器取得提示数据的文本输入组件
 * @param trigger 触发生成AjaxValidator组件对象的对象(Node对象，可以是对象id)
 * @param url 向服务器请求数据的url(字符串对象)
 * @param parameters  向服务器提交的参数(js对象)
 */
var AjaxValidator = Class.create();
AjaxValidator.INPUT_EVENT = 'input';
AjaxValidator.TRIGGER_BLUR = 'triggerblur';
AjaxValidator.prototype = {
	className: 'tip_01',
	panel:undefined,
	data:'',
	//inputListener:Prototype.emptyListener,
	flag:0,
	fireInput: function(event) {
		var result = this.beforInterceptor();
		//debug('beforInterceptor result--->' + this.result);
		if(this.flag){
			clearTimeout(this.flag);
			this.flag = 0;
		}
		if(!result){
			this.flag = setTimeout((function(){
				this.unrender();
				this.data = this.fetchData();
				if(this.data.trim().length) this.render();
			}).bind(this),3000);
		}else{
				this.unrender();
				this.data = result||'';
				if(this.data.trim().length) this.render();
		}
	},
	fireTriggerBlur: function(event){
		var result = this.beforInterceptor();
		//debug('---beforInterceptor result--->' + this.result);
		if(this.flag){
			clearTimeout(this.flag);
			this.flag = 0;
		}
		this.unrender();
		if(!result){
			this.data = this.fetchData();
		}else{
			this.data = result||'';
		}
		if(this.data.trim().length) this.render();
	},
    initialize: function(trigger,url,parameters,beforInterceptor) {
    	this.trigger = $(trigger);
    	this.url = url;
		this.beforInterceptor = beforInterceptor||(function(){return false;});
    	if(parameters instanceof String || typeof (parameters) == 'string'){
    		this.parameters = parameters;
    	}else{
    		this.parameters = $H(parameters).toQueryString();
    	}
    	
    	this.addEventListener(AjaxValidator.INPUT_EVENT,this.fireInput);
    	this.addEventListener(AjaxValidator.TRIGGER_BLUR,this.fireTriggerBlur);
    },
    fetchData: function(data){
    	if(data){
    		return data;
    	}else{
    		//if(this.trigger.value ||this.trigger.value.trim().length){
				var myAjax = new Ajax.Request(this.url,
				    {method:"post",
				     parameters:this.parameters+'&'+(this.trigger.name||this.trigger.id)+'='+encodeURIComponent(this.trigger.value)+'&responseType=json',
				     onException:Prototype.emptyFunction,
				     asynchronous:false});
				//debug(this.parameters+'&'+(this.trigger.name||this.trigger.id)+'='+this.trigger.value+'&responseType=json');
			    var jsonString=myAjax.transport.responseText;
				data = (new Function("return "+jsonString)).call(this);
				
				myAjax=null;
				return data.errorMessage||'';
    		//}
    		return '';
    	}
    },
    addEventListener: function(type,listener,useCapture){
    	switch(type){
    		case AjaxValidator.TRIGGER_BLUR:
    		    Event.observe(this.trigger,'blur',listener.bind(this),useCapture);
    		    break;
    		case AjaxValidator.INPUT_EVENT:
    		    Event.observe(this.trigger,'keyup',listener.bind(this),useCapture);
    		    break;
    		default:
    		    alert('The ' + type + ' is not existent!');
    	}
    },
    removeEventListener: function(type,listener,useCapture){
    	switch(type){
    		case AjaxValidator.TRIGGER_BLUR:
    		    Event.stopObserving(this.trigger,'blur',listener.bind(this),useCapture);
    		    break;
    		case AjaxValidator.INPUT_EVENT:
    		    Event.stopObserving(this.trigger,'keyup',listener,useCapture);
    		    break;
    		default:
    		    alert('The ' + type + ' is not existent!');
    	}
    },
    render: function(){
    	this.panel = document.createElement('span');
    	Element.addClassName(this.panel,this.className);
    	var text = document.createTextNode(this.data||'error');
    	this.panel.appendChild(text);
    	this.trigger.parentNode.appendChild(this.panel);
    	//Element.setOpacity(this.panel,1);
    	//Element.shadow(this.panel,true);
    	return this.panel;
    },
    unrender: function(){
		//this.removeEventListener(CueBox.CLICK_OUT_EVENT,this.clickoutListener);
    	if(this.panel){
    		Element.remove(this.panel);
    	    this.panel = undefined;
    	}
    	this.data='';
    	//Element.undoPositioned(this.trigger.parentNode);
    }
    
}